#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int p, e, i, d;
	int res;
	int n = 1;
	int max = 21252;

	while (scanf("%d %d %d %d", &p, &e, &i, &d) != EOF && p != -1) {
		res = ((5544*p + 14421*e + 1288*i) % max - d + max) % max;
		if (res == 0) res = max;
		printf("Case %d: the next triple peak occurs in %d days.\n", n++, res);
	}

	return 0;
}